package com.huluobo.a2111douyindemo.login;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.mvvm_base.mvvm.base.model.BaseRepo;
import com.huluobo.mvvm_base.mvvm.bean.UserBeanResult;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

/**
 * Created by LC on 2024/2/22.
 * MutableLiveData 动态数据,MVVM模式通过观察LiveData的变化来赋值
 */
public class LoginRepo extends BaseRepo {
    public void login(RequestBody body, MutableLiveData<UserBeanResult> success, MutableLiveData<String> failed) {
        apiService.login(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UserBeanResult userBeanResult) {
                        //给LiveData赋值,相当于P层中成功的回调
                        success.setValue(userBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        //失败的赋值,相当于P层中失败的回调
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
